import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

/// 数字输入框 带加减号
/// [onChange]:数字改变回调
/// [defaultNum]:初始化默认数字
/// [minNum]:最小支持数字
/// [maxNum]:最大支持数字
class NumInputWidget extends StatefulWidget {
  final Function onChange;
  final String defaultNum;
  final String minNum;
  final String maxNum;

  NumInputWidget({this.defaultNum = "0", this.onChange, this.minNum, this.maxNum});

  @override
  State<StatefulWidget> createState() {
    return NumInputWidgetState(defaultNum);
  }
}

class NumInputWidgetState extends State<NumInputWidget> {
  final String defaultNum;
  NumInputWidgetState(this.defaultNum);
  static String textStr = "0";
  TextEditingController _controller = TextEditingController.fromValue(
      TextEditingValue(
          // 设置内容
          text: textStr,
          // 保持光标在最后
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream,
              offset: textStr != null ? textStr.length : 0))));

  @override
  void initState() {
    super.initState();
    textStr = defaultNum;
    this._controller.text = defaultNum;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 120,
      height: 30,
      child: Row(
        children: <Widget>[
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              try {
                if (_controller.text.isNotEmpty) {
                  if (int.parse(_controller.text) > 0) {
                    textStr = (int.parse(_controller.text) - 1).toString();
                    _controller.text = textStr;
                    if (widget.onChange != null) {
                      widget.onChange(_controller.text);
                    }
                  }
                } else {
                  _controller.text = defaultNum;
                  if (widget.onChange != null) {
                    widget.onChange(_controller.text);
                  }
                }
              } catch (e) {
                print(e);
                _controller.text = defaultNum;
                if (widget.onChange != null) {
                  widget.onChange(_controller.text);
                }
              }
            },
            child: Image(
              image: AssetImage(
                "packages/flutter_dk_form/images/dk_form/delete.png",
              ),
              width: 28,
              height: 30,
              fit: BoxFit.scaleDown,
            ),
          ),
          Container(
            width: 0.5,
            height: 30,
            color: Color(0xFFD8D8D8),
          ),
          Container(
            width: 61,
            height: 30,
            child: TextField(
              controller: _controller,
              maxLines: 1,
              keyboardType: TextInputType.number,
              inputFormatters: <TextInputFormatter>[
                WhitelistingTextInputFormatter.digitsOnly,
                LengthLimitingTextInputFormatter(6),
                NotStartWith0InputFormatter(),
              ],
              style: TextStyle(fontSize: 15, color: Color(0xFF000000)),
              decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(top: 5)),
              textAlign: TextAlign.center,
              onChanged: (data) {
                textStr = data;
                if (widget.onChange != null) {
                  widget.onChange(data);
                }
//                _controller.text = data;
              },
            ),
          ),
          Container(
            width: 0.5,
            height: 30,
            color: Color(0xFFD8D8D8),
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              try {
                textStr = (int.parse(_controller.text) + 1).toString();
                _controller.text = textStr;
                if (widget.onChange != null) {
                  widget.onChange(_controller.text);
                }
              } catch (e) {
                print(e);
                _controller.text = defaultNum;
                if (widget.onChange != null) {
                  widget.onChange(_controller.text);
                }
              }
            },
            child: Image(
              image: AssetImage(
                "packages/flutter_dk_form/images/dk_form/add.png",
              ),
              width: 28,
              height: 30,
              fit: BoxFit.scaleDown,
            ),
          ),
        ],
      ),
      decoration: BoxDecoration(
          color: Color(0xFFFFFFFF),
          border: Border.all(width: 0.5, color: Color(0xFFD6D5D5)),
          borderRadius: BorderRadius.all(Radius.circular(1))),
    );
  }
}

class NotStartWith0InputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return newValue != null &&
            newValue.text.isNotEmpty &&
            !newValue.text.startsWith("0")
        ? newValue
        : TextEditingValue();
  }
}
